package com.example.tourpal.widget;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.tourpal.MainActivity;
import com.example.tourpal.PartnerSettingActivity;
import com.example.tourpal.R;
import com.example.tourpal.SpotDateSetActivity;
import com.example.tourpal.SpotOverViewActivity;
import com.example.tourpal.model.Consume;

public class SpotItem extends LinearLayout {

	private static final String UNDER_LINE = "-";
	public int spotId;
	public TextView spotName;
	public TextView followSet;
	public TextView spotDate;
	public TextView consumAdd;

	private String followSetContent;
	private boolean isFollow;

	private MainActivity mActivity;
	private Context c;

	public SpotItem(Context context) {
		this(context, null);
		// TODO Auto-generated constructor stub
	}

	public SpotItem(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
		// TODO Auto-generated constructor stub
	}

	public SpotItem(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		c = context;
		mActivity = (MainActivity) context;

		setPadding(13, 13, 13, 13);
		setBackgroundColor(Color.WHITE);
		setLayoutParams(new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		setOrientation(LinearLayout.VERTICAL);

		RelativeLayout relativeLayout = new RelativeLayout(context);
		LinearLayout.LayoutParams relativeLayoutParams = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

		spotName = new TextView(context);
		spotName.setId(mActivity.getAddSpotId());
		spotName.setText(R.string.default_edit_spot_name);
		spotName.setTextSize(15);
		spotName.setTextColor(Color.GRAY);
		spotName.setPadding(0, 0, 0, 8);
		spotName.setGravity(Gravity.CENTER);
		Drawable spotNameD = getResources().getDrawable(R.drawable.local);
		spotNameD.setBounds(0, 0, spotNameD.getMinimumWidth(),
				spotNameD.getMinimumHeight());
		spotName.setCompoundDrawables(spotNameD, null, null, null);
		spotName.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				TextView tv = (TextView) v;
				Intent intent = new Intent();
				intent.putExtra("spotId", tv.getId());
				intent.setClass(c, SpotOverViewActivity.class);
				mActivity.startActivityForResult(intent,
						SpotOverViewActivity.RESULT_BACK);
				mActivity.overridePendingTransition(R.anim.sub_enter,
						R.anim.main_exit);
			}
		});
		RelativeLayout.LayoutParams spotNameParams = new RelativeLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		spotNameParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

		followSet = new TextView(context);
		followSet.setId(mActivity.getAddSpotId());
		followSet.setTextSize(15);

		updateFollowButton(isFollow);
		followSet.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				TextView tv = (TextView) v;
				Intent intent = new Intent();
				intent.putExtra("spotId", tv.getId());
				intent.putExtra("spotName", spotName.getText());
				if (isFollow) {
					intent.putExtra("followSet", isFollow);
					intent.putExtra("followContent", followSetContent);
				}
				intent.setClass(c, PartnerSettingActivity.class);
				mActivity.startActivityForResult(intent,
						PartnerSettingActivity.RESULT_BACK);
				mActivity.overridePendingTransition(R.anim.sub_enter,
						R.anim.main_exit);
			}
		});
		RelativeLayout.LayoutParams followSetParams = new RelativeLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		followSetParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

		relativeLayout.addView(spotName, spotNameParams);
		relativeLayout.addView(followSet, followSetParams);

		spotDate = new TextView(context);
		spotDate.setId(mActivity.getAddSpotId());
		String defaultDate = new SimpleDateFormat(c.getText(
				R.string.date_format_2).toString(), Locale.getDefault())
				.format(new Date());
		spotDate.setText(defaultDate + UNDER_LINE + defaultDate);
		spotDate.setTextSize(12);
		spotDate.setTextColor(0xffa9a9a9);
		spotDate.setPadding(0, 0, 0, 8);
		spotDate.setGravity(Gravity.CENTER);
		Drawable spotDateD = getResources().getDrawable(R.drawable.date);
		spotDateD.setBounds(0, 0, spotDateD.getMinimumWidth(),
				spotDateD.getMinimumHeight());
		spotDate.setCompoundDrawables(spotDateD, null, null, null);
		spotDate.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				TextView tv = (TextView) v;
				Intent intent = new Intent();
				intent.putExtra("spotId", tv.getId());
				intent.setClass(c, SpotDateSetActivity.class);
				mActivity.startActivityForResult(intent,
						SpotDateSetActivity.RESULT_BACK);
			}
		});
		LinearLayout.LayoutParams spotDateParams = new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

		ImageView line = new ImageView(context);
		line.setBackgroundColor(0xffe6e6e6);
		line.setPadding(0, 0, 0, 8);
		LinearLayout.LayoutParams lineParams = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, 2);

		consumAdd = new TextView(context);
		consumAdd.setText(R.string.default_edit_consum);
		consumAdd.setTextSize(15);
		consumAdd.setTextColor(0xff666666);
		consumAdd.setGravity(Gravity.START);
		consumAdd.setPadding(0, 8, 0, 0);
		Drawable consumAddD = getResources().getDrawable(R.drawable.add_consum);
		consumAddD.setBounds(0, 0, consumAddD.getMinimumWidth(),
				consumAddD.getMinimumHeight());
		consumAdd.setCompoundDrawables(consumAddD, null, null, null);
		consumAdd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				addConsunItem(null);
			}

		});
		LinearLayout.LayoutParams consumAddParams = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

		addView(relativeLayout, relativeLayoutParams);
		addView(spotDate, spotDateParams);
		addView(line, lineParams);
		addView(consumAdd, consumAddParams);
	}

	protected void updateFollowButton(boolean f) {
		int textRes = R.string.disfollow_button;
		int textColor = 0xff888888;
		int IconRes = R.drawable.disfollow;
		if (f) {
			textRes = R.string.follow_button;
			textColor = 0xff2ba47b;
			IconRes = R.drawable.follow;
		}
		followSet.setText(textRes);
		followSet.setTextColor(textColor);
		Drawable followSetD = getResources().getDrawable(IconRes);
		followSetD.setBounds(0, 0, followSetD.getMinimumWidth(),
				followSetD.getMinimumHeight());
		followSet.setCompoundDrawables(followSetD, null, null, null);
	}

	@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
	public void addConsunItem(Consume consume) {
		final EditText consumItem = new EditText(c);
		consumItem.setId(consumItem.hashCode());
		consumItem.setBackground(null);
		consumItem.setTextSize(15);

		Drawable consumItemD = null;
		if (consume == null) {
			consumItem.setHint(getConsumHint(7));
			consumItemD = getResources().getDrawable(R.drawable.other);
		} else {
			consumItem.setHint(getConsumHint(consume.getType()));
			consumItem.setText(consume.getDescription());
			consumItemD = getResources().getDrawable(
					getIndicate(consume.getType()));
		}

		consumItem.setHintTextColor(0xffa9a9a9);
		consumItemD.setBounds(0, 0, consumItemD.getMinimumWidth(),
				consumItemD.getMinimumHeight());
		consumItem.setCompoundDrawables(consumItemD, null, null, null);
		consumItem.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean isFocus) {
				if (isFocus) {
					Toast.makeText(c, v.getId() + " isFocus",
							Toast.LENGTH_SHORT).show();
					mActivity.setInputTarget(v);
					mActivity.showInputView(v);
					mActivity
							.updateIndicateUI(getUIIndicate(getConsumeHint(consumItem
									.getHint().toString())));
				}
			}

		});
		consumItem.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mActivity.showInputView(v);
				mActivity
						.updateIndicateUI(getUIIndicate(getConsumeHint(consumItem
								.getHint().toString())));
			}
		});
		consumItem.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				Toast.makeText(c, "delete consumItem", Toast.LENGTH_SHORT)
						.show();
				removeView(v);
				return true;
			}
		});
		consumItem.requestFocus();
		mActivity.showInputView(consumItem);
		addView(consumItem, getChildCount() - 1);
	}

	private int getConsumeHint(String hint) {
		if (hint.equals(getResources().getString(
				R.string.default_consum_for_food))) {
			return 1;
		}
		if (hint.equals(getResources().getString(
				R.string.default_consum_for_live))) {
			return 2;
		}
		if (hint.equals(getResources()
				.getString(R.string.default_consum_for_go))) {
			return 3;
		}
		if (hint.equals(getResources().getString(
				R.string.default_consum_for_swim))) {
			return 4;
		}
		if (hint.equals(getResources().getString(
				R.string.default_consum_for_amuse))) {
			return 5;
		}
		if (hint.equals(getResources().getString(
				R.string.default_consum_for_buy))) {
			return 6;
		} else {
			return 7;
		}

	}

	private int getConsumHint(int type) {
		int resId = R.string.default_consum_for_other;

		switch (type) {
		case 1:
			resId = R.string.default_consum_for_food;
			break;
		case 2:
			resId = R.string.default_consum_for_live;
			break;
		case 3:
			resId = R.string.default_consum_for_go;
			break;
		case 4:
			resId = R.string.default_consum_for_swim;
			break;
		case 5:
			resId = R.string.default_consum_for_amuse;
			break;
		case 6:
			resId = R.string.default_consum_for_buy;
			break;
		case 7:
			resId = R.string.default_consum_for_other;
			break;
		default:
			break;
		}
		return resId;
	}

	private int getUIIndicate(int type) {
		int resId = R.id.otherIndicate;

		switch (type) {
		case 1:
			resId = R.id.foodIndicate;
			break;
		case 2:
			resId = R.id.liveIndicate;
			break;
		case 3:
			resId = R.id.goIndicate;
			break;
		case 4:
			resId = R.id.swimIndicate;
			break;
		case 5:
			resId = R.id.amuseIndicate;
			break;
		case 6:
			resId = R.id.buyIndicate;
			break;
		case 7:
			resId = R.id.otherIndicate;
			break;
		default:
			break;
		}
		return resId;
	}

	private int getIndicate(int index) {
		int resId = R.drawable.other;

		switch (index) {
		case 1:
			resId = R.drawable.food;
			break;
		case 2:
			resId = R.drawable.live;
			break;
		case 3:
			resId = R.drawable.go;
			break;
		case 4:
			resId = R.drawable.swim;
			break;
		case 5:
			resId = R.drawable.amuse;
			break;
		case 6:
			resId = R.drawable.buy;
			break;
		case 7:
			resId = R.drawable.other;
			break;
		default:
			break;
		}
		return resId;
	}

	public int getSpotId() {
		return spotId;
	}

	public void setSpotId(int spotId) {
		this.spotId = spotId;
	}

	public TextView getSpotName() {
		return spotName;
	}

	public TextView getSpotDate() {
		return spotDate;
	}

	public TextView getFollowSet() {
		return followSet;
	}

	public String getFollowSetContent() {
		return followSetContent;
	}

	public void setFollowSetContent(String followSetContent) {
		this.followSetContent = followSetContent;
	}

	public int getFollow() {
		return isFollow ? 1 : 0;
	}

	public void setFollow(boolean isFollow) {
		this.isFollow = isFollow;
		updateFollowButton(isFollow);
	}
}
